/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pbv.azsg.dao;

import java.util.Date;

import com.pbv.azsg.domain.Fuelling;
import com.pbv.azsg.domain.FuellingStatus;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class FuellingDAOImpl implements IFuellingDAO {
    
    @Autowired
    protected SessionFactory sessionFactory;
    
    @Override    
    public Fuelling getFuelling(Long id){
        return (Fuelling) 
          this.sessionFactory
              .getCurrentSession()
              .createCriteria(Fuelling.class)
              .add(Restrictions.eq("id", id))
              .list()
              .get(0);
    }
    
    @Override    
    public Fuelling makeFuelling(Fuelling fuelling){
        
        fuelling.setErrmsg(null);
        fuelling.setFuelled(null);
        fuelling.setFuelVolume(null);
        fuelling.setFuelled(null);
        fuelling.setRequested( new Date() );
        fuelling.setStatus( FuellingStatus.NEW );
        this.sessionFactory.getCurrentSession().save(fuelling);        
               
        return fuelling;
    }
    
    @Override    
    public Fuelling solveFuelling(Long id, FuellingStatus status, String errmsg)  throws ExceptionBadStatus {
        Fuelling result = getFuelling( id );
        if ( status.equals( FuellingStatus.NEW ) || status.equals( FuellingStatus.EXPIRED ) ) throw new ExceptionBadStatus();
        return null;
    }
    
}
